<?php 
namespace app\agent\controller;

use app\common\model\user\UserActFinance;
use app\common\model\user\Users;
use app\common\model\user\UserActParam;
use think\Exception;
use app\common\model\user\UserActCash;
use think\Db;
use app\common\controller\Finance;
use app\common\model\user\UserActRecharge;
use app\common\controller\Base64;
use think\Config;
use app\common\model\coin\CoinList;

class Act extends Base
{
	/**
	 * 财务列表
	 * @return \think\response\View
	 */
	public function finance()
	{
		$data = $this->request->param('');
		$mobile = !empty($data['mobile']) ? $data['mobile'] : '';
		$start_time = !empty($data['start_time']) ? $data['start_time'] : '';
		$end_time = !empty($data['end_time']) ? $data['end_time'] : '';
		$where = ['user_id' => $this->agent_id,'type' => 2]; //初始化查询条件
		$where_all = ['user_id' => $this->agent_id,'type' => 2];
		if($mobile) {
			$user_id = Users::where(['user_name' => $mobile])->value('id');
			$where['from_id'] =$user_id;
			$where_all['from_id'] = $user_id;
		}
		$where_day = $where_all;
		if($start_time && $end_time) {
			if(strtotime($end_time) > strtotime($start_time)) {
				$where['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
				$where_all['create_time'] = ['between',[strtotime($start_time),strtotime($end_time)]];
			}
		}
		$list = UserActFinance::where($where)->order('id desc')->paginate(15,false,[
				'query' => $data
		]);
		//获取数据汇总
		$start = strtotime(date('Y-m-d').'00:00:00');
		$end = strtotime(date('Y-m-d').'23:59:59');
		$where_day['create_time'] = ['between',[$start,$end]];
		$all = UserActFinance::where($where_all)->sum('price');
		$day = UserActFinance::where($where_day)->sum('price');
		$page = $list -> render();
		$list = $list->toArray();
		foreach ($list['data'] as &$rows) {
			$rows['from_info'] = Users::get($rows['from_id']);
		}
		$this->assign('mobile',$mobile);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('all',$all);
		$this->assign('day',$day);
		return view();
	}
	
	/**
	 * 提币列表
	 * @return \think\response\View
	 */
	public function cash()
	{
		//获取代理信息
		$account = Users::get($this->agent_id);
		//获取提现手续费
		$param = $this->getActParam($this->agent_id);
		//获取提币记录
		$list = UserActCash::where(['user_id' => $this->agent_id])->paginate();
		$this->assign('account',$account);
		$this->assign('param',$param);
		$this->assign('list',$list);
		return view();
	}
	
	/**
	 * 提币
	 * @throws Exception
	 */
	public function cashDao()
	{
		$data = $this->request->param('');
		Db::startTrans();
		try {
			if(!$data['price'])throw new Exception('请输入提币金额');
			if(!$data['address'])throw new Exception('请输入提币地址');
			$account = Users::where(['id' => $this->agent_id])->value('bail');
			//获取提现手续费
			$param = $this->getActParam($this->agent_id);
			$fee = $param['cash_fee'];
			if($data['price'] >= ($account - $param['min_bai_num']) )throw new Exception('账户余额不足');
			if($data['price'] < $param['min_cash'])throw new Exception('最小提币金额不能小于'.$param['min_cash']);
			if($data['price'] > $param['max_cash'])throw new Exception('最大提币金额不能大于'.$param['max_cash']);
			$data = [
					'user_id' => $this->agent_id,
					'fee' => $fee,
					'price' => $data['price'],
					'real_price' => $data['price'] - $fee,
					'address' => $data['address'],
					'state' => 0,
					'create_time' => time(),
					'update_time' => time()
			];
			UserActCash::create($data);
			//扣除用户账户
			Users::where(['id' => $this->agent_id])->setDec('bail',$data['price']);
			//添加财务
			$finance = new Finance();
			$finance->addActFinance($this->agent_id, $this->agent_id, -$data['price'], '提币'.$data['price'], 2);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return $this->error($e->getMessage());
		}
		return $this->success('提币成功');
	}
	
	/**
	 * 充币记录
	 * @return \think\response\View
	 */
	public function recharge()
	{
		$list = UserActRecharge::where(['user_id' => $this->agent_id])->paginate();
		$page = $list->render();
		$this->assign('list',$list);
		$this->assign('page',$page);
		return view();
	}
	
	/**
	 * 充币
	 * @return \think\response\View
	 */
	public function insert()
	{
		$address = CoinList::get(5);
		$this->assign('address',$address->address);
		return view();
	}
	
	/**
	 * 充币
	 * @throws Exception
	 */
	public function insertDao()
	{
		$data = $this->request->param('');
		try {
			if(!$data['price']) throw new Exception('请输入充币金额');
			if(!$data['address']) throw new Exception('请输入充币地址');
			if(!$data['pay_img']) throw new Exception('请上传打款截图');
			$base = new Base64();
			$data['pay_img'] = $base->up_base64($data['pay_img'], Config::get('path.recharge'));
			if(!$data['pay_img']) throw new Exception('请上传打款截图');
			$info = [
					'user_id' => $this->agent_id,
					'price' => $data['price'],
					'address' => $data['address'],
					'pay_img' => $data['pay_img'],
					'state' => 0,
					'create_time' => time(),
					'update_time' => time()
			];
			UserActRecharge::create($info);
		} catch (Exception $e) {
			return $this->error($e->getMessage());
		}
		return $this->success('提交成功');
	}
	/**
	 * 获取代理参数
	 * @param unknown $user_id
	 * @return unknown
	 */
	public function getActParam($user_id)
	{
		$param = UserActParam::where(['user_id' => $user_id])->find();
		if(!$param) {
			$param = UserActParam::where(['id' => 1])->find();
		}
		return $param;
	}
}